class ContentController < ApplicationController
  require_role "editor", :for => :changeArticleState
  require_role "editor",:for => :showArticleApproval
  require "find"
  #acts_as_friendly_param :creation_user

  #before_filter :initVersion


  def initVersion
    @contents[:title] = "testing"
    if false
      redirect_to :controller => :site, :action => :index if params[:id] == nil
      @content_ver_id = params[:content_version_id]
      @items_per_page = ROWS_PER_PAGE
      @current_user_id = current_user.id

      @categories = Category.find(:all, :conditions => ["content_type_id = :condTypeID", { :condTypeID => @contypeid } ],
                                  :order => "name")
      if @content_ver_id
        @content_edit = ContentVersion.find(:first,
                                            :joins => "as cv inner join content_raws cr on cv.content_raw_id = cr.id",
                                            :select => "cr.category_id, cv.subject AS title, cv.body, cv.content_raw_id",
                                            :conditions => ["(cv.id = :condCVID)",
                                                            { :condCVID => @content_ver_id } ])

      end
    end
  end


  def index
    @contypeid = params[:id]
    @items_per_page = ROWS_PER_PAGE
    @content_types = ContentType.find(:all, :order => "id")
    @categories = Category.find(:all, :conditions => ["content_type_id = :condTypeID", { :condTypeID => @contypeid } ],
                                :order => "name")

    if params[:selCategory] != "ALL" && params[:selCategory]
      # Search some category

      if params[:schTitle] && params[:schTitle].length > 0
        # Search some title
        @contents = ContentRaw.paginate(:per_page => @items_per_page, :page => params[:page],
                                        :joins => "as cr inner join content_versions cv on cr.id = cv.content_raw_id
                                                  inner join (select content_raw_id, MAX(version_date) AS last_version from content_versions group by content_raw_id) cv2
                                                  on cv.content_raw_id = cv2.content_raw_id and cv.version_date = cv2.last_version 
                                                  inner join users u on cr.creation_user = u.id
                                                  inner join categories c on cr.category_id = c.id
                                                  left outer join (select refers_to, count(id) AS total from content_raws group by refers_to having refers_to IS NOT NULL) rp
                                                  on cr.id = rp.refers_to",
                                        :select => "cv.id as article_id, cv.subject AS title, c.name AS categoryname,u.id as user_id, u.name AS author, cv.editorial_status_date AS publishdate, CASE WHEN rp.total IS NULL THEN 0 ELSE rp.total END AS total_reply",
                                        :conditions => ["(cr.refers_to IS NULL) AND (cv.editorial_status = 'approved') AND
                                                  (cr.content_type_id = :condTypeID) AND (cr.category_id = :condCatID) AND (UPPER(cv.subject) like UPPER(:condTitle))",
                                                        { :condTypeID => @contypeid, :condCatID => params[:selCategory], :condTitle => params[:schTitle].strip + "%" } ],
                                        :order => "cv.editorial_status_date DESC")
      else
        # Search all title
        @contents = ContentRaw.paginate(:per_page => @items_per_page, :page => params[:page],
                                        :joins => "as cr inner join content_versions cv on cr.id = cv.content_raw_id
                                                  inner join (select content_raw_id, MAX(version_date) AS last_version from content_versions group by content_raw_id) cv2
                                                  on cv.content_raw_id = cv2.content_raw_id and cv.version_date = cv2.last_version 
                                                  inner join users u on cr.creation_user = u.id
                                                  inner join categories c on cr.category_id = c.id
                                                  left outer join (select refers_to, count(id) AS total from content_raws group by refers_to having refers_to IS NOT NULL) rp
                                                  on cr.id = rp.refers_to",
                                        :select => "cv.id as article_id, cv.subject AS title, c.name AS categoryname, u.id as user_id, u.name AS author, cv.editorial_status_date AS publishdate, CASE WHEN rp.total IS NULL THEN 0 ELSE rp.total END AS total_reply",
                                        :conditions => ["(cr.refers_to IS NULL) AND (cv.editorial_status = 'approved') AND
                                                  (cr.content_type_id = :condTypeID) AND (cr.category_id = :condCatID)", { :condTypeID => @contypeid, :condCatID => params[:selCategory] } ],
                                        :order => "cv.editorial_status_date DESC")
      end

    else
      # Search ALL categories

      if params[:schTitle] && params[:schTitle].length > 0
        # Search some title
        @contents = ContentRaw.paginate(:per_page => @items_per_page, :page => params[:page],
                                        :joins => "as cr inner join content_versions cv on cr.id = cv.content_raw_id
                                                  inner join (select content_raw_id, MAX(version_date) AS last_version from content_versions group by content_raw_id) cv2
                                                  on cv.content_raw_id = cv2.content_raw_id and cv.version_date = cv2.last_version 
                                                  inner join users u on cr.creation_user = u.id
                                                  inner join categories c on cr.category_id = c.id
                                                  left outer join (select refers_to, count(id) AS total from content_raws group by refers_to having refers_to IS NOT NULL) rp
                                                  on cr.id = rp.refers_to",
                                        :select => " cv.id as article_id, cv.subject AS title, c.name AS categoryname, u.id as user_id, u.name AS author, cv.editorial_status_date AS publishdate, CASE WHEN rp.total IS NULL THEN 0 ELSE rp.total END AS total_reply",
                                        :conditions => ["(cr.refers_to IS NULL) AND (cv.editorial_status = 'approved') AND
                                                  (cr.content_type_id = :condTypeID) AND (UPPER(cv.subject) like UPPER(:condTitle))",
                                                        { :condTypeID => @contypeid, :condTitle => params[:schTitle].strip + "%" } ],
                                        :order => "cv.editorial_status_date DESC")
      else
        # Search all title
        @contents = ContentRaw.paginate(:per_page => @items_per_page, :page => params[:page],
                                        :joins => "as cr inner join content_versions cv on cr.id = cv.content_raw_id
                                                  inner join (select content_raw_id, MAX(version_date) AS last_version from content_versions group by content_raw_id) cv2
                                                  on cv.content_raw_id = cv2.content_raw_id and cv.version_date = cv2.last_version 
                                                  inner join users u on cr.creation_user = u.id
                                                  inner join categories c on cr.category_id = c.id
                                                  left outer join (select refers_to, count(id) AS total from content_raws group by refers_to having refers_to IS NOT NULL) rp
                                                  on cr.id = rp.refers_to",
                                        :select => " cv.id as article_id, cv.subject AS title, c.name AS categoryname, u.id as user_id, u.name AS author, cv.editorial_status_date AS publishdate, CASE WHEN rp.total IS NULL THEN 0 ELSE rp.total END AS total_reply",
                                        :conditions => ["(cr.refers_to IS NULL) AND (cv.editorial_status = 'approved') AND
                                                  (cr.content_type_id = :condTypeID)", { :condTypeID => @contypeid } ],
                                        :order => "cv.editorial_status_date DESC")
      end
    end
  end

  # Credits http://www.java-samples.com/showtutorial.php?tutorialid=1126
  def view_source
    if VIEW_SOURCE != "DISABLED"
      @filenames = Array.new
      Dir["./app/controllers/*.html.erb"].each{ |f|
        @filenames.push f
      }
      excludes = [".svn"] #exclude the .svn directory which may be present
      Find.find("./app/views/") do |path|
        if FileTest.directory?(path)
          if excludes.include?(File.basename(path))
            Find.prune       # Don't look any further into this directory.
          else
            next
          end
        else
          @filenames.push path
        end
      end
      @contents = get_file_contents(@filenames)
    end
  end


  def manage_article
    @contypeid = params[:id]

    # using contentTypeId to render custom fields for different contents
    case @contypeid
      when "1"
        @do = "Create Article"
        @type = "Article"
        @title = "Title"
        @body =  "Body"
      when "2"
      when "3"
      when "4"
      when "5"
        @do = "Post a question"
        @title = "Question"
        @type = "Question"
        @body =  "Description"
    end


    @content_ver_id = params[:content_version_id]
    @items_per_page = ROWS_PER_PAGE
    @current_user_id = current_user.id

    @categories = Category.find(:all, :conditions => ["content_type_id = :condTypeID", { :condTypeID => @contypeid } ],
                                :order => "name")
    if @content_ver_id
      @content_edit = ContentVersion.find(:first,
                                          :joins => "as cv inner join content_raws cr on cv.content_raw_id = cr.id",
                                          :select => "cr.category_id, cv.subject AS title, cv.body, cv.content_raw_id",
                                          :conditions => ["(cv.id = :condCVID)",
                                                          { :condCVID => @content_ver_id } ])
    end

    if params[:save]
      ContentRaw.transaction do

        if params[:hidCase] == "Delete"

          # DELETE data in content_versions table.
          # And case last version : delete data in content_raws table, else don't delete it.
          @count_version = ContentVersion.find(:first,
                                               :select => "count(*) AS total_version",
                                               :group => "content_raw_id",
                                               :conditions => ["(content_raw_id = :condCRID)",
                                                               { :condCRID => params[:hidConRawID] } ])

          ContentVersion.delete(params[:hidConVerID])
          if @count_version.total_version == 1
            ContentRaw.delete(params[:hidConRawID])
          end

          flash[:notice] = "Article was successfully deleted."

        else

          if params[:hidCase] == "Edit"
            # UPDATE data into content_raws table.
            ContentRaw.update(params[:hidConRawID], :category_id => params[:selCategory])

            # INSERT data into content_versions table.
            @cv = ContentVersion.new

            @cv.content_raw_id = params[:hidConRawID]

          else
            # INSERT data into content_raws table.
            @cr = ContentRaw.new

            @cr.content_type_id = @contypeid
            @cr.creation_user = @current_user_id
            @cr.creation_date = Time.now
            #@cr.release_date
            #@cr.expiration_time
            @cr.mime_type = "text/html"
            @cr.category_id = params[:selCategory]

            @cr.save

            # INSERT data into content_versions table.
            @cv = ContentVersion.new

            @cv.content_raw_id = @cr.id
          end

          @cv.version_date = Time.now
          @cv.subject = params[:title]
          @cv.body = params[:body]

          if @contypeid == "5"
            @cv.editorial_status = "approved"
            @cv.editorial_status_date = Time.now
          else
            @cv.editorial_status = "submitted"
          end
          #@cv.editor_id
          #@cv.editorial_status_date

          if @cv.save
            if params[:hidCase] == "Edit"
              flash[:notice] = @type  + " was successfully updated."
            else
              flash[:notice] = @type + " was successfully created."
              users = User.find :all, :joins => "as u inner join roles_users as ru on ru.user_id = u.id inner join roles r on r.id=ru.role_id",
                                :conditions => ["r.name='editor'"],
                                :select => "u.email as email, u.id as user_id"
              users.each do |u|
                MailArticle.deliver_article_submitted u.user_id, @cv.id.to_s
              end
            end
          end
        end
      end
    end



    @contents = ContentRaw.paginate(:per_page => @items_per_page, :page => params[:page],
                                    :joins => "as cr inner join content_versions cv on cr.id = cv.content_raw_id
                                              inner join (select content_raw_id, MAX(version_date) AS last_version from content_versions group by content_raw_id) cv2
                                              on cv.content_raw_id = cv2.content_raw_id and cv.version_date = cv2.last_version 
                                              inner join categories c on cr.category_id = c.id",
                                    :select => "cv.id AS content_version_id, cv.subject AS title, cv.body, c.name AS categoryname, cr.created_at AS createdate, cv2.last_version, cv.editorial_status, cv.editorial_status_date",
                                    :conditions => ["(cr.refers_to IS NULL) AND (cr.content_type_id = :condTypeID) AND (cr.creation_user = :condUserID)",
                                                    { :condTypeID => @contypeid, :condUserID => @current_user_id } ],
                                    :order => "cv.editorial_status_date DESC, cv2.last_version DESC")


  end


  def manage_category
    @contypeid = params[:id]
    @contypeid = "1" if @contypeid.nil?
    @do = "Add "

    # using contentTypeId to render custom fields for different contents
    case @contypeid
      when "1"
        @do = "Create article "
        @type = "Article"
        @title = "Category"
        @body =  "Body"
      when "2"
      when "3"
      when "4"
      when "5"
        @do = "Create question "
        @title = "Category"
        @type = "Question"
        @body =  "Description"        
    end


    @content_ver_id = params[:content_version_id]
    @items_per_page = ROWS_PER_PAGE
    @current_user_id = current_user.id

    @categories = Category.find(:all, :conditions => ["content_type_id = :condTypeID", { :condTypeID => @contypeid } ],
                                :order => "name")
    #if @content_ver_id
    # @content_edit = ContentVersion.find(:first,
    #                                    :joins => "as cv inner join content_raws cr on cv.content_raw_id = cr.id",
    #                                   :select => "cr.category_id, cv.subject AS title, cv.body, cv.content_raw_id",
    #                                  :conditions => ["(cv.id = :condCVID)",
    #                                                 { :condCVID => @content_ver_id } ])
    #end

    if params[:save]
      Category.transaction do
        if params[:hidCase].to_s.downcase == "delete"
          # DELETE data in content_versions table.
          # And case last version : delete data in content_raws table, else don't delete it.
          #@count_version = ContentVersion.find(:first,
          #                                    :select => "count(*) AS total_version",
          #                                   :group => "content_raw_id",
          #                                  :conditions => ["(content_raw_id = :condCRID)",
          #                                                 { :condCRID => params[:hidConRawID] } ])

          Category.delete(params[:category_id])
          #if @count_version.total_version == 1
          # ContentRaw.delete(params[:hidConRawID])
          #end
          flash[:notice] = "Article category was successfully deleted."
        else

          if params[:hidCase] == "Edit"
            @do = "Edit "
            # UPDATE data into content_raws table.
            @title = @do + " " + @title
            cat = Category.find_by_id(params[:category_id])
            cat.name = params[:category_name]
            #cat.save
            # INSERT data into content_versions table.
            #@cv = ContentVersion.new
            #@cv.content_raw_id = params[:hidConRawID]

          else
            cat = Category.new :name => params[:category_name], :content_type_id => (ContentType.find_by_name "articles").id
            #cat.save
            # INSERT data into content_raws table.
            #@cr = ContentRaw.new
            #@cr.content_type_id = @contypeid
            #@cr.creation_user = @current_user_id
            #@cr.creation_date = Time.now
            #@cr.release_date
            #@cr.expiration_time
            #@cr.mime_type = "text/html"
            #@cr.category_id = params[:selCategory]

            #@cr.save

            # INSERT data into content_versions table.
            #@cv = ContentVersion.new

            #@cv.content_raw_id = @cr.id
          end

          #@cv.version_date = Time.now
          #@cv.subject = params[:title]
          #@cv.body = params[:body]

          #if @contypeid == "5"
          # @cv.editorial_status = "approved"
          #@cv.editorial_status_date = Time.now
          #else
          # @cv.editorial_status = "submitted"
          #end
          #@cv.editor_id
          #@cv.editorial_status_date

          if cat.save
            if params[:hidCase] == "Edit"
              flash[:notice] = @type  + " category was successfully updated."
            else
              flash[:notice] = @type + " category was successfully created."
              #users = User.find :all, :joins => "as u inner join roles_users as ru on ru.user_id = u.id inner join roles r on r.id=ru.role_id",
              #                :conditions => ["r.name='editor'"],
              #               :select => "u.email as email, u.id as user_id"
              #users.each do |u|
              # MailArticle.deliver_article_submitted u.user_id, @cv.id.to_s
              #end
            end
          end
        end
      end
    end



    # @contents = ContentRaw.paginate(:per_page => @items_per_page, :page => params[:page],
    #                                :joins => "as cr inner join content_versions cv on cr.id = cv.content_raw_id
    #                                         inner join (select content_raw_id, MAX(version_date) AS last_version from content_versions group by content_raw_id) cv2
    #                                        on cv.content_raw_id = cv2.content_raw_id and cv.version_date = cv2.last_version
    #                                       inner join categories c on cr.category_id = c.id",
    #                            :select => "cv.id AS content_version_id, cv.subject AS title, cv.body, c.name AS categoryname, cr.created_at AS createdate, cv2.last_version, cv.editorial_status, cv.editorial_status_date",
    #                           :conditions => ["(cr.refers_to IS NULL) AND (cr.content_type_id = :condTypeID) AND (cr.creation_user = :condUserID)",
    #                                          { :condTypeID => @contypeid, :condUserID => @current_user_id } ],
    #                         :order => "cv.editorial_status_date DESC, cv2.last_version DESC")


  end




  def showArticle
    @content_type_id = (ContentType.find_by_name "articles").id
    @article_id = params[:article_id]

    if is_editor?
      @edit_flag = true
      @article = ContentRaw.find(:first, :joins => " inner join content_versions cv on content_raws.id = cv.content_raw_id
                                                 inner join users u on content_raws.creation_user = u.id
                                                 inner join categories c on content_raws.category_id = c.id",
                                 :select => "content_raws.id as crid, cv.subject AS title, cv.editorial_status as state, cv.body as body, c.name AS categoryname, u.id as user_id, u.name AS author, content_raws.creation_date AS publishdate",
                                 :conditions => ["(content_raws.refers_to IS NULL) AND
                                              (content_raws.content_type_id = :condTypeID) and (cv.id = :article_id)", { :condTypeID => @content_type_id, :article_id => @article_id} ])
    else
      @edit_flag = false
      @article = ContentRaw.find(:first, :joins => " inner join content_versions cv on content_raws.id = cv.content_raw_id
                                                 inner join users u on content_raws.creation_user = u.id
                                                 inner join categories c on content_raws.category_id = c.id",
                                 :select => "content_raws.id as crid, cv.subject AS title, cv.body as body, c.name AS categoryname, u.id as user_id, u.name AS author, content_raws.creation_date AS publishdate",
                                 :conditions => ["(content_raws.refers_to IS NULL) AND (cv.editorial_status = 'approved') AND
                                              (content_raws.content_type_id = :condTypeID) AND (cv.id = :cv_id)", { :condTypeID => @content_type_id, :cv_id => @article_id} ])
    end

    @article[:publishdate] = @article[:publishdate].to_datetime.strftime('%Y-%m-%d %H:%M:%S')

    if @article.nil?
      render_404
    end
    @c_id = (ContentType.find_by_name "user_comments").id
    @comments = ContentRaw.find(:all, :joins => " inner join content_versions cv on content_raws.id = cv.content_raw_id
                                                   inner join users u on content_raws.creation_user = u.id ",
                                :select => "cv.body AS body, u.name AS author, u.id as user_id, content_raws.creation_date AS publishdate",
                                :conditions => ["(content_raws.refers_to IS NOT NULL) AND (cv.editorial_status = 'approved') AND
                                                (content_raws.content_type_id = :condTypeID)  AND (content_raws.refers_to = :article_id)", { :condTypeID => @c_id, :article_id => @article[:crid]} ],
                                :order => "cv.editorial_status_date DESC")




  end

  # Problem Set 5
  # Action for
  #   1. Displaying a particular question and its comments

  def showQuestion
    @content_type_id = (ContentType.find_by_name "questions").id
    @question_id = params[:question_id]

    @question = ContentRaw.find(:first, :joins => " inner join content_versions cv on content_raws.id = cv.content_raw_id
                                                 inner join users u on content_raws.creation_user = u.id
                                                 inner join categories c on content_raws.category_id = c.id",
                                :select => "content_raws.id as crid, cv.subject AS title, cv.editorial_status as state, cv.body as body, c.name AS categoryname, u.id as user_id, u.name AS author, content_raws.creation_date AS publishdate",
                                :conditions => ["(cv.id = :question_id) AND (content_raws.refers_to IS NULL) AND
                                              (content_raws.content_type_id = :condTypeID)", { :question_id => @question_id, :condTypeID => @content_type_id} ])

    @question[:publishdate] = @question[:publishdate].to_datetime.strftime('%Y-%m-%d %H:%M:%S')

    if @question.nil?
      render_404
    end
    @c_type_id = (ContentType.find_by_name "user_comments").id
    @comments = ContentRaw.find(:all, :joins => " inner join content_versions cv on content_raws.id = cv.content_raw_id
                                                   inner join users u on content_raws.creation_user = u.id ",
                                :select => "cv.body AS body, u.id as user_id, u.name AS author, content_raws.creation_date AS publishdate",
                                :conditions => ["(content_raws.refers_to IS NOT NULL) AND (cv.editorial_status = 'approved') AND
                                                (content_raws.content_type_id = :condTypeID) AND (content_raws.refers_to = :question_id)", { :condTypeID => @c_type_id, :question_id => @question[:crid]} ],
                                :order => "cv.editorial_status_date DESC")




  end

  def showArticleApproval
    @status = params[:status]
    case when @status == "submitted"
      @status = "submitted"
      when @status == "rejected"
        @status = "rejected"
      when @status == "approved"
        @status = "approved"
      when @status == "expired"
        @status = "expired"
      else
        @status = "submitted"
    end
    @content_type_id = (ContentType.find_by_name "articles").id
    @articles = ContentRaw.find(:all, :joins => " inner join content_versions cv on content_raws.id = cv.content_raw_id
                                                 inner join users u on content_raws.creation_user = u.id
                                                 inner join categories c on content_raws.category_id = c.id",
                                :select => "cv.id as article_id, cv.subject AS title, c.name AS categoryname,u.id as user_id, u.name AS author, content_raws.creation_date AS publishdate",
                                :conditions => ["(content_raws.refers_to IS NULL) AND (cv.editorial_status = :status) AND
                                              (content_raws.content_type_id = :condTypeID)", { :status => @status, :condTypeID => @content_type_id} ])
  end

  def changeArticleState
    @article = params[:article]

    case when @article[:do] == "approve"
      # TODO FInd the latest version of the article
      cv = ContentVersion.find_by_id @article[:article_id]
      if cv.nil?
        break        eate
      else
        cv.editorial_status = "approved"
        cv.editorial_status_date = Time.now
        if cv.save
          flash[:notice] = "Article updated"
          MailArticle.deliver_article_approved current_user.id, @article[:article_id]
        else
          flash[:notice] = "Article couldn't be updated"
        end
      end

      when @article[:do] == "disapprove"
        # TODO FInd the latest version of the article
        cv = ContentVersion.find_by_id @article[:article_id]
        if cv.nil?
          break
        else
          cv.editorial_status = "rejected"
          cv.editorial_status_date = Time.now
          if cv.save
            flash[:notice] = "Article updated"
            MailArticle.deliver_article_disapproved current_user.id, @article[:article_id]
          else
            flash[:notice] = "Article couldn't be updated"
          end
        end

    end
    redirect_to :action => :showArticle, :article_id =>  @article[:article_id]
  end

  def addComment
    if logged_in?
      @comment = params[:comment]
      @content_type_id = (ContentType.find_by_name "user_comments").id
      @type=@comment[:type]
      ct = ContentType.find @type
      if ct==nil
        flash[:notice] = "Comment couldn't be saved."
        redirect_back_or_default :controller => :site, :action => :index
      end

      # case 1 means Articles
      case when ct.name.downcase == "articles"

        if @comment.nil? || @comment[:article_id].nil?

          flash[:notice] = "Comment couldn't be saved."
          redirect_back_or_default :controller => :site, :action => :index
        else
          cr  = ContentRaw.new(:content_type_id => @content_type_id, :refers_to => (ContentVersion.find @comment[:article_id]).content_raw_id, :creation_user => current_user.id)
          if cr.save
            cv = ContentVersion.new :content_raw_id => cr.id, :body => @comment["comment"], :editorial_status => "approved"
            cv.save
            @done = true
          end
          if @done
            flash[:notice] = "Comment added"
            cr = ContentRaw.find(:first, :joins => (" as cr inner join content_versions as cv on cv.content_raw_id = cr.id "),
                                 :select => "cr.creation_user as user_id",
                                 :conditions => ["cv.id = :version_id",{:version_id => @comment[:article_id]}]
            )
            MailArticle.deliver_article_comment cr.user_id, @comment[:article_id]
          else
            flash[:notice] = "Comment couldn't be saved."
          end
          redirect_to :action => :showArticle, :article_id =>  @comment[:article_id]
        end


        # case Questions
        when ct.name.downcase =="questions"
          if @comment.nil? || @comment[:question_id].nil?
            flash[:notice] = "Comment couldn't be saved."
            redirect_back_or_default :controller => :site, :action => :index
          else
            cr  = ContentRaw.new(:content_type_id => @content_type_id, :refers_to => (ContentVersion.find @comment[:question_id]).content_raw_id, :creation_user => current_user.id)
            if cr.save
              cv = ContentVersion.new :content_raw_id => cr.id, :body => @comment["comment"], :editorial_status => "approved"
              cv.save
              @done = true
            end
            if @done
              flash[:notice] = "Your answer was added."
              cr = ContentRaw.find(:first, :joins => (" as cr inner join content_versions as cv on cv.content_raw_id = cr.id "),
                                   :select => "cr.creation_user as user_id",
                                   :conditions => ["cv.id = :version_id",{:version_id => @comment[:question_id]}]
              )
              MailArticle.deliver_question_answer cr.user_id, @comment[:question_id]
            else
              flash[:notice] = "Your comment couldn't be saved. Try again later or contact our admin."
            end
            #redirect_back_or_default :action => :showQuestion,:question_id =>  @comment[:question_id]

            redirect_to( :controller => :content, :action => :showQuestion, :question_id =>  @comment[:question_id])
          end

      end
    end
  end

  protected
  def get_file_contents(files)
    data = Array.new
    for f in files
      file_handle = File.new(f, "r")
      file_contents = " "
      while (line = file_handle.gets)
        file_contents = file_contents.concat(line)
      end
      file_handle.close
      data.push(file_contents)
    end
    return data
  end
end
